﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Diagnostics;

namespace Muccu.Contracts
{
    [DataContract]
    public class Response
    {
        public Response()
        {
            this.JsonRpc = "2.0";
        }

        [DataMember(Name = "jsonrpc")]
        public string JsonRpc { get; set; }

        [DataMember(Name = "id")]
        public int Id { get; set; }

        private object _result;
        [DataMember(Name = "result", IsRequired = false)]
        public object Result
        {
            get { return _result; }
            set { _result = value; }
        }

        private Error _error;
        [DataMember(Name = "error", IsRequired = false)]
        public Error Error
        {
            get { return _error; }
            set { _error = value; }
        }

        public string ToJson()
        {
            if (_error == null)
            {
                return string.Format(@"{{""jsonrpc"": ""{0}"", ""id"": {1}, ""result"": {2}}}", JsonRpc, Id, Result.ToJson());
            }
            else
            {
                Tracer.WriteError(Error);
                return string.Format(@"{{""jsonrpc"": ""{0}"", ""id"": {1}, ""error"": {2}}}", JsonRpc, Id, Error.ToJson());
            }
        }

        public static Response CreateError(int id, int code, string message, object data)
        {
            return new Response { Id = id, Error = new Error { Code = code, Message = message, Data = data } };
        }

        #region Predefined errors
        public static Response E_CUSTOM_ERRORS_OFF = new Response { Error = new Error { Code = -32700, Message = "Runtime error -  An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed. To enable the details of this specific error message to be viewable on the local server machine, please create a <customErrors> tag within a \"web.config\" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its \"mode\" attribute set to \"RemoteOnly\". To enable the details to be viewable on remote machines, please set \"mode\" to \"Off\"." } };
        /// <summary>
        /// Parse error. Invalid JSON
        /// </summary>
        public static Response E_32700 = new Response { Error = new Error { Code = -32700, Message = "Parse error. Invalid JSON" } };
        /// <summary>
        /// Invalid Request
        /// </summary>
        public static Response E_32600 = new Response { Error = new Error { Code = -32600, Message = "Invalid Request" } };
        public static Response E_32601 = new Response { Error = new Error { Code = -32601, Message = "Method not found" } };
        /// <summary>
        /// Invalid params
        /// </summary>
        public static Response E_32602 = new Response { Error = new Error { Code = -32602, Message = "Invalid params" } };
        public static Response E_32603 = new Response { Error = new Error { Code = -32603, Message = "Internal error" } };

        /// <summary>
        /// Ticket expired
        /// </summary>
        public static Response E_32100 = new Response { Error = new Error { Code = -32100, Message = "Ticket expired" } };
        /*        
                -32099..-32000 Server error. Reserved for implementation-defined server-errors. 
                        */

        public static Response E_401 = new Response { Error = new Error { Code = 401, Message = "Unauthorized request" } };
        #endregion
    }
}
